<?php

namespace app\data\controller\record;

use app\data\service\CityUserRelationService;
use app\data\service\StringParse;
use think\admin\Controller;
use think\admin\service\AdminService;

/**
 * 师傅手动完成工单
 * Class CarRecordComplete
 * @package app\data\controller\record
 */
class CarRecordComplete extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'data_record';

    /**
     * 师傅手动完成工单
     * @auth true
     * @return void
     */
    public function search()
    {
        $keyword = $this->request->request('keyword');
        if (empty($keyword)) {
            $this->success('', []);
        }
        $adminId = AdminService::instance()->getUserId();
        $response = $this->app->db->name($this->table)
            ->field('id,username,plate_number')
            ->where('uuid_city', $adminId)
            ->where(function ($where) use ($keyword) {
                return $where->where('username', 'like', "%{$keyword}%")->whereOr('plate_number', 'like', "%{$keyword}%");
            })->select()->toArray();

        foreach ($response as &$item) {
            $item['url'] = config('fei_shu.url') . "/admin.html?show=car_record_complete&id={$item['id']}#/data/record.car_record_complete/edit.html?id={$item['id']}";

        }
        $this->success($adminId, $response);
    }

    /**
     * 表单结果处理
     * @param boolean $state
     */
    protected function _form_result(bool $state, array $data)
    {
        if ($state) {
            if ($this->isMobile()) {
                $this->success('保存成功！');
            } else {
                $this->success('保存成功！');
            }
        }
    }

    /**
     * 师傅手动完成工单编辑
     * @auth true
     * http://youkejia.test/data/record.car_record_complete/edit.html?show_id=1000&id=1000
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $this->_form($this->table, 'form_mobile');
    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {
        $this->title = '信息采集管理';
        if ($this->request->isPost()) {
            // 检查登录账号是否出现重复
            $record = $this->app->db->name('data_record')->where('id', $data['id'])->findOrEmpty();

//            if ($data['paint_status'] != 1) {
//                $this->error('喷漆完成为否，不可保存');
//            }

            if (!empty($data['phone'])) {
                [$digits, $chinese] = StringParse::instance()->splitDigitsAndChinese($data['car_vehicle_type']);
                $carVehicleTypeId = StringParse::instance()->carVehicleType($digits, $chinese);
                if (!empty($carVehicleTypeId)) {
                    $data['car_vehicle_type_id'] = $carVehicleTypeId;
                }

                if ($data['paint_status'] == 1) {
                    if (empty($data['painter'])) {
                        $this->error('喷漆师傅不能为空');
                    }

                    if (in_array($record['paint_status'], [-1, 0])) {
                        $data['time_complete'] = date('Y-m-d H:i:s');
                    } else {
                        $data['time_complete'] = null;
                    }
                    $data = CityUserRelationService::instance()->calcPaintPrice($record, $data);
                }
            }

            // 处理邀请情况
            if (empty($record["invite_remark"])) {
                // 首次预约
                $data["invite_remark"] = $data["invite_info"];
                $data["time_invite"] = date("Y-m-d H:i:s");
            } else {
                // 最近预约
                if (empty($record["invite_remarks"])) {
                    // 最近预约为空直接覆盖
                    $data["invite_remarks"] = $data["invite_info"];
                } else {
                    // 不为空直接拼接
                    $data["invite_remarks"] = $record["invite_remarks"] . "\n" . $data["invite_info"];
                }
                $data["time_invites"] = $record["time_invites"] . "\n" . date("Y-m-d H:i:s");
            }
        } else {
            $info = $this->app->db->name('data_record')->where('id', input("id"))->findOrEmpty();
            $inviteInfo = "";
            if (!empty($info)) {
                $inviteInfo = !empty($info["invite_remarks"]) ? $info["invite_remarks"] : $info["invite_remark"];
            }
            $this->assign("invite_info", $inviteInfo);
        }
    }
}